package xin.nick;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.StaticHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Nick
 * @since 2023/7/23
 */
@Slf4j
public class AppMain {

    public static void main(String[] args) {
        log.info("开始启动HTTP服务器");
        Vertx vertx = Vertx.vertx();
        Router router = Router.router(vertx);
        HttpServer server = vertx.createHttpServer();

        // 设置静态网站资源路径
        router.route().handler(StaticHandler.create());

        // 添加 /hello 路由
        router.get("/hello").handler(AppMain::handleHello);

        // 创建HTTP服务器并监听端口
        server.requestHandler(router)
                .listen(8080, result -> {
                    if (result.succeeded()) {
                        log.info("Server started on port 8080");
                    } else {
                        log.info("Failed to start server");
                    }
                });

        log.info("构建完毕");
        log.info("http://localhost:8080");
    }

    // 处理 /hello 请求
    private static void handleHello(RoutingContext routingContext) {
        routingContext.response().end("Hello");
    }

}
